<template>
<view>
    <view class="isnone" v-show="isNone">
      <text>暂时还没有哦！请稍后再试……</text>
    </view>
		<view class="goods-list">
      <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        isNone:false,
				queryObj:{//请求参数对象
          query:'',//查询关键词
          cid:'',//分类ID
          pagenum: 1,//页数索引
          pagesize: 10//每页长度
        },
        goodsList:[],
        total:0,
        isLoading:false
			};
		},
    onLoad(options) {
      this.queryObj.query = options.query || '';
      this.queryObj.cid = options.cid || '';
      this.getGoodsList();
    },
    methods:{
      async getGoodsList(cb){
        this.isLoading = true;//打开节流阀
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj);
        this.isLoading = false;//关闭节流阀
        cb && cb()
        if(res.meta.status !== 200) return uni.$showMsg();
        this.goodsList = [...this.goodsList,...res.message.goods];
        this.total = res.message.total;
        if(this.goodsList.length === 0) this.isNone = true;
      },
      gotoDetail(goods){
        // console.log(goods);
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('已经到底了哦！');
      if(this.isLoading) return;
      this.queryObj.pagenum++;
      this.getGoodsList()
    },
    onPullDownRefresh() {
      //重置数据
      this.queryObj.pagenum = 1;
      this.isLoading = false;
      this.goodsList = [];
      this.total = 0;
      
      this.getGoodsList(()=>{uni.stopPullDownRefresh()})
    }
	}
</script>

<style lang="scss">
  .isnone{
    text-align: center;
    font-size: 20px;
    color: #c00000;
  }
</style>
